package com.ruoyi.worker.domain.workAttendance;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.worker.domain.workAttendance.dto.AttendanceMonthRecordDataDto;
import lombok.Data;
import lombok.ToString;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName AttendanceMonthRecord
 * @Description 月考勤
 * @Author leizhang
 * Date 2021/11/24 2:56 下午
 * @Version 1.0
 **/
@Data
@ToString
public class AttendanceMonthRecord extends BaseEntity {
    /**考勤日期**/
    private String workTime;
    /**员工姓名**/
    private String staffName;
    /**班组id**/
    private int deptId;
    /**身份证**/
    private String idCard;
    /**班组名称**/
    private String deptName;
    /**班次名称**/
    private String shiftName;
    /**班次id**/
    private int shiftId;
    /**早退次数**/
    private int leaveEarly;
    /**普通迟到**/
    private int normallyLate;
    /**一般迟到**/
    private int usuallyLate;
    /**严重迟到**/
    private int severelyLate;
    /**旷工**/
    private int absenteeism;
    /**异常**/
    private int abnormalAttendance;
    /**应出勤天数**/
    private int needSign;
    /**实际出勤**/
    private int actualSign;
    /**组别**/
    private String groupName;
    /**岗位名称**/
    private String postName;
    /**白班出勤天数**/
    private int daySignTime;
    /**夜班出勤天数**/
    private int nightSignTime;
    /**通班出勤天数**/
    private int allDaySignTime;
    /**休息次数**/
    private int numberOfRest;
    private int shiftTime;

    /**
     * 排班记录
     **/
    private List<RzScheduleStaffPlan> staffPlans;

    private List<AttendanceMonthRecordDataDto> attendanceMonthRecordDataDtos;

    /** 日考勤记录**/
    private List<AttendanceDailyRecord> listOfAttendanceDailRecords;
    /**
     * @Description : 计算月考勤计算
     * @Author : leizhang
     * @Date 10:05 上午 2021/12/1
     * @param
     * @return void
     **/
    public void calculateAttendanceDat() {
        if (!listOfAttendanceDailRecords.isEmpty()) {
            //1.计算当月 普通迟到次数 一般迟到次数 严重迟到次数 早退次数 旷工次数
            listOfAttendanceDailRecords.forEach(attendanceDailyRecord -> {
                if (attendanceDailyRecord.getNormallyLate() > 0) {
                    //普通迟到次数
                    normallyLate += attendanceDailyRecord.getNormallyLate();
                }
                if (attendanceDailyRecord.getUsuallyLate() > 0) {
                    //一般迟到
                    usuallyLate += attendanceDailyRecord.getUsuallyLate();
                }
                if (attendanceDailyRecord.getSeverelyLate() > 0) {
                    //严重迟到次数
                    severelyLate += attendanceDailyRecord.getSeverelyLate();
                }
                if (attendanceDailyRecord.getLeaveEarly() > 0) {
                    //早退次数
                    leaveEarly += attendanceDailyRecord.getLeaveEarly();
                }
                if (attendanceDailyRecord.getAbsenteeism() > 0) {
                    //旷工次数
                    absenteeism += attendanceDailyRecord.getAbsenteeism();
                }
            });
            //2.休息天数统计
            List<AttendanceDailyRecord> attendanceTypwOfRest = listOfAttendanceDailRecords.stream().filter(attendanceDailyRecord -> attendanceDailyRecord.getAttendanceType() == 2).collect(Collectors.toList());
            numberOfRest = attendanceTypwOfRest.size();
            //3.白班 通班 夜班 出勤天数 统计
            Map<String, List<AttendanceDailyRecord>> groupByShiftName = listOfAttendanceDailRecords.parallelStream().filter(attendanceDailyRecord -> attendanceDailyRecord.getAttendanceType() == 1  &&  (attendanceDailyRecord.getAbnormalAttendance() ==3 || attendanceDailyRecord.getAbnormalAttendance() ==2)).collect(Collectors.groupingBy(AttendanceDailyRecord::getShiftName));
            groupByShiftName.forEach((shiftName,listValue)->{
                if ("白班".equals(shiftName)) {
                    //统计白班人数
                    daySignTime = listValue.size();
                }
                if ("夜班".equals(shiftName)) {
                    nightSignTime = listValue.size();
                }
                if ("通班".equals(shiftName)) {
                    allDaySignTime = listValue.size();
                }
            });
            //实际出勤天数
            actualSign = daySignTime + nightSignTime + allDaySignTime;
            // 排班
            needSign = null == staffPlans ? 0 : staffPlans.size();
        }
    }
    public AttendanceMonthRecord() {
    }
}
